var orderList = {};
var OrderList = function () {
    this.dictMap = {}; //数据字典
    this.rowData; //选中行数据
    this.aStationText = "发出站";
    this.arriveStationText = "到站";
    //初始化信息
    this.init = function () {
        var paramMap = layuiFunciton.getURLParamMap();
        if (paramMap) {
            $("#tranType").val(paramMap["tranType"])
        }
        //初始化数据字典
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sys/dictVal/getByTypeCode',
            method: 'get',
            maskState: false,
            success: function (data) {
                data.forEach(function (obj, index) {
                    orderList.dictMap[obj.valCode] = obj.valName;
                });
            }
        });
        //初始化搜索条件 日期范围组件
        layuiFunciton.layTime({
            elem: '#orderDateStart'
            , format: 'yyyy-MM-dd'
        });
        layuiFunciton.layTime({
            elem: '#orderDateEnd'
            , format: 'yyyy-MM-dd'
        });
        //初始化搜索条件业务员
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sysUser/findUserList',
            data: {page: 1, limit: 99999},
            maskState: false,         //是否开启遮罩
            success: function (data) {
                var optionHtml = '<option value="">请选择</option>';
                data.forEach(function (obj, index) {
                    optionHtml += '<option value="' + obj.name + '">' + obj.name + '</option>';
                });
                $("#salesman").html(optionHtml);
            }
        });
        //初始化数据字典
        var businessTypes = "YWLX_T";
        if (paramMap) {
            $("#tranType").val(paramMap.tranType);
            if (paramMap["tranType"] == "1") {//铁路运输
                businessTypes = "YWLX_T";
                //发出站
                layuiFunciton.selectDict("aStation", "ZD");
                //到站
                layuiFunciton.selectDict("arriveStation", "ZD");
            } else if (paramMap["tranType"] == "2") {//水路运输
                businessTypes = "YWLX_S";
                $("#aStationText").text("发出港");
                $("#arriveStationText").text("到港");
                orderList.aStationText = "发出港";
                orderList.arriveStationText = "到港";
                //发出港
                layuiFunciton.selectDict("aStation", "GK");
                //到港
                layuiFunciton.selectDict("arriveStation", "GK");
            } else if (paramMap["tranType"] == "3") {//公路运输
                orderList.arriveStationText = "卸货地点";
                //发出站
                layuiFunciton.selectDict("aStation", "ZD");
                //卸货地点
                layuiFunciton.selectDict("arriveStation", "ZHDD");
                businessTypes = "YWLX_G";
            }
        }
        //业务类型
        layuiFunciton.selectDict("businessTypes", businessTypes);
        //品名
        layuiFunciton.selectDict("ofTheGoods", "Of_the_goods");
        //箱型
        layuiFunciton.selectDict("aBox", "XX");
        //发货单位
        layuiFunciton.selectDict("deliveryUnit", "FHDW");
        //收货人
        layuiFunciton.selectDict("receivingUnit", "SHDW");
        //结算人
        layuiFunciton.selectDict("settlementPeople", "JSR");
        //司机
        layuiFunciton.selectDict("driver", "SJ");
        //车牌
        layuiFunciton.selectDict("licensePlate", "CP");
        //费用类型
        layuiFunciton.selectDict("costType", "FYLX");
        //收款单位
        //layuiFunciton.selectDict("typeReceivingUnit", "SKDW");
        //装货地点
        layuiFunciton.selectDict("standby1", "ZHDD");
    };
	this.trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    //初始化表格
    this.initTalbe = function () {
        var paramMap = layuiFunciton.getURLParamMap();
        layuiFunciton.tableInit({
            toolbar: '.btn',
            tableId: 'orderListTableId',
            url: layuiFunciton.URL + '/wb/schedule/types/costStatistics',
            id: 'orderListId',
            searchFromId: 'searchForm',
            tableFilter: 'orderListFilter',  //表格监听名称,
            cols: [[
                {field: 'salesman', width: 100, title: '业务员', sort: true},
                {field: 'orderCode', width: 120, title: '订单编号', sort: true}
                , {field: 'orderDate', width: 120, title: '日期', sort: true}
                , {
                    field: 'businessTypes', title: '业务类型', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.businessTypes]){
                            return orderList.dictMap[row.businessTypes];
                        }else{
                            return "";
                        }
                    }
                }
                , {
                    field: 'ofTheGoods', title: '品名', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.ofTheGoods]){
                            return orderList.dictMap[row.ofTheGoods];
                        }else{
                            return "";
                        }
                    }
                }
                , {
                    field: 'aBox', title: '箱型', sort: true,
                    templet: function (row) {
                        return orderList.dictMap[row.aBox];
                    }
                }
                , {field: 'cartonNo', title: '箱号', sort: true}
                , {
                    field: 'deliveryUnit', title: '发货单位', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.deliveryUnit]) {
                            return orderList.dictMap[row.deliveryUnit];
                        } else {
                            return "";
                        }
                    }
                }, {
                    field: 'standby1', title: '装货地点', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.standby1]) {
                            return orderList.dictMap[row.standby1];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'aStation', title: orderList.aStationText, sort: true,hide: (paramMap["tranType"] == "3"),
                    templet: function (row) {
                        if (orderList.dictMap[row.aStation]) {
                            return orderList.dictMap[row.aStation];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'arriveStation', title: orderList.arriveStationText, sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.arriveStation]) {
                            return orderList.dictMap[row.arriveStation];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'arriveStation', title: '到站承运', sort: true, hide: paramMap["tranType"] != "1",
                    templet: function (row) {
                        if (orderList.dictMap[row.standby3]) {
                            return orderList.dictMap[row.standby3];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'receivingUnit', title: '收货人', sort: true/*,
                     templet: function (row) {
                     return orderList.dictMap[row.receivingUnit];
                     }*/
                }
                , {
                    field: 'settlementPeople', title: '结算人', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.settlementPeople]) {
                            return orderList.dictMap[row.settlementPeople];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'whetherSelfTransport', align: "center", title: '自运输', sort: true,
                    templet: function (row) {
                        if (row.whetherSelfTransport == 1) {
                            return "<input type='checkbox' lay-skin='primary'  disabled checked/>";
                        } else {
                            return "<input type='checkbox' lay-skin='primary' disabled />";
                        }
                    }
                }
                , {
                    field: 'driver', title: '司机', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.driver] && row.whetherSelfTransport == 1) {
                            return orderList.dictMap[row.driver];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'licensePlate', title: '车牌', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.licensePlate]  && row.whetherSelfTransport == 1) {
                            return orderList.dictMap[row.licensePlate];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'isInvoice', align: "center", title: '需开票', sort: true,
                    templet: function (row) {
                        if (row.isInvoice == 1) {
                            return "<input type='checkbox' lay-skin='primary' disabled checked/>";
                        } else {
                            return "<input type='checkbox' lay-skin='primary' disabled/>";
                        }
                    }
                }, {
                    field: 'costType', title: '费用类型', sort: true,
                    templet: function (row) {
                        if (row.wbScheduleTypes) {
                            return orderList.dictMap[row.wbScheduleTypes.costType];
                        } else {
                            return "";
                        }
                    }
                }, {
                    field: 'amountPayable', title: '应付金额', sort: true,
                    templet: function (row) {
                        if (row.wbScheduleTypes.amountPayable) {
                            return Number(row.wbScheduleTypes.amountPayable).toFixed(2);
                        } else {
                            return "0.00";
                        }
                    }
                }, {
                    field: 'amountRealPay', title: '实付金额', sort: true,
                    templet: function (row) {
                        if (row.wbScheduleTypes.amountRealPay) {
                            return Number(row.wbScheduleTypes.amountRealPay).toFixed(2);
                        } else {
                            return "0.00";
                        }
                    }
                }, {
                    field: 'receivingUnit', title: '收款单位', sort: true,
                    templet: function (row) {
                        if (row.wbScheduleTypes.receivingUnit) {
                            return orderList.dictMap[row.wbScheduleTypes.receivingUnit];
                        } else {
                            return "";
                        }
                    }
                }
            ]],
            tools: {
                export: function () {
                    location.href = layuiFunciton.URL + '/wb/schedule/types/export?' + $("#searchForm").serialize();
                },
                exportPage: function (data,obj) {
                    //获取当前页
                    var recodePage = $(".layui-laypage-curr .layui-laypage-em").siblings().text();
                    //获取当前页条数
                    var recodeLimit = $(".layui-laypage-limits").find("option:selected").val();
                    location.href = layuiFunciton.URL + '/wb/schedule/types/export?page='+recodePage+'&limit='+recodeLimit+'&' + $("#searchForm").serialize();
                }
            }
        });
    };
    //获取当月第一天
    this.getCurrentMonthFirst = function () {
        var date = new Date();
        date.setDate(1);
        return orderList.dateFtt("yyyy-MM-dd", date);
    };
    //获取当月最后一天
    this.getCurrentMonthLast = function () {
        var date = new Date();
        var currentMonth = date.getMonth();
        var nextMonth = ++currentMonth;
        var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        return orderList.dateFtt("yyyy-MM-dd", new Date(nextMonthFirstDay - oneDay));
    };
    //时间格式化
    this.dateFtt = function (fmt, date) {
        var o = {
            "M+": date.getMonth() + 1,     //月份
            "d+": date.getDate(),     //日
            "h+": date.getHours(),     //小时
            "m+": date.getMinutes(),     //分
            "s+": date.getSeconds(),     //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()    //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
    this.event = function () {
        $("#search").click(function () {
            if($("#orderDateStart").val().length == 0 || $("#orderDateEnd").val().length == 0){
                if(!($("#orderDateStart").val().length == 0 && $("#orderDateEnd").val().length == 0)){
                    layuiFunciton.warn('开始时间和结束时间必填！');
                    return;
                }
            }
            layuiFunciton.refresh('searchForm', 'orderListId');
        });
        $("#reload").click(function () {
            $("#orderDateStart").val("");
            $("#orderDateEnd").val("");
        });
    };
};
orderList = new OrderList();
orderList.init();
orderList.initTalbe();
orderList.event();